use std::{borrow::Borrow, cell::RefCell, collections::HashMap, rc::Rc};

use ash::vk;

use crate::{vulkan::VkContext, VK_LOGP};

use self::object::{static_mesh::StaticMeshObject, ObjectTrait};

use super::render::RenderSystem;

pub mod object;

pub struct SceneSystemCreateInfo<'a> {
    pub render_system       : &'a RenderSystem
}

pub struct SceneSystem {
    // pub cube : Cube
    object_counter      : usize,

    object_map          : HashMap<usize, Rc<RefCell<dyn ObjectTrait>>>
}

impl SceneSystem {
    pub fn new(scene_ci: &SceneSystemCreateInfo) -> Self {
        let object_map = HashMap::new();

        Self {
            object_counter  : 0,
            object_map      : object_map
        }
    }

    pub fn add_object(&mut self, render_system: &mut RenderSystem, object: Rc<RefCell<dyn ObjectTrait>>) {
        let id = self.object_counter;
        self.object_counter += 1;

        // object
        self.object_map.insert(object.clone().borrow_mut().get_id(), object);
    }

    pub fn get_object_model_mat(&self, id: usize) -> cgmath::Matrix4<f32> {
        match self.object_map.get(&id) {
            Some(object)        => object.borrow_mut().get_model_mat(),
            None                => VK_LOGP!("Can't Find Object {id}"),
        }
    }

    pub fn tick(&mut self, delta_time: f32) {
        for (id, object) in &mut self.object_map {
            object.borrow_mut().tick(delta_time);
        }
    }
}